package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.mapper.CustomerTraceHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.shiro.MyRealm;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService {
    @Autowired
    private CustomerTraceHistoryMapper CustomerTraceHistoryMapper;
    @Autowired
    private MyRealm myRealm;

    @Override
    public void saveOrUpdate(CustomerTraceHistory entity) {
        if(entity.getId()!=null)
        {
            CustomerTraceHistoryMapper.updateByPrimaryKey(entity);
            //更新完成后清除缓存
            myRealm.clearCached();
        }else{
            entity.setInputUser(UserContext.getCurrentEmp());
            CustomerTraceHistoryMapper.insert(entity);
        }
    }

    @Override
    public PageInfo<CustomerTraceHistory> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"his.input_time DESC");
        List<CustomerTraceHistory> list = CustomerTraceHistoryMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

}
